package com.bhdx.blog.controller.admin;

/**
 * @author bhdx
 * @date 2020/6/16-15:23
 */

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;


import com.alibaba.fastjson.JSONObject;
import com.bhdx.blog.entity.Blog;
import com.bhdx.blog.entity.PageBean;
import com.bhdx.blog.lucene.BlogIndex;
import com.bhdx.blog.service.BlogService;
import com.bhdx.blog.util.DateJsonValueProcessor;
import com.bhdx.blog.util.ResponseUtil;
import com.bhdx.blog.util.StringUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JsonConfig;
import net.sf.json.*;



@Controller
@RequestMapping("/admin/blog")
public class BlogAdminController {

    @Resource
    private BlogService blogService;

    @Resource
    private BlogIndex blogIndex;

    //后台分页查询博客信息
    @RequestMapping("/listBlog")
    public String listBlog(
            @RequestParam(value="page", required=false)String page,
            @RequestParam(value="rows", required=false)String rows,
            Blog s_blog,
            HttpServletResponse response) throws Exception {

        PageBean pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(rows));
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("title", StringUtil.formatLike(s_blog.getTitle())); //模糊查询
        map.put("start", pageBean.getStart());
        map.put("pageSize", pageBean.getPageSize());
        List<Blog> blogList = blogService.listBlog(map);
        Long total = blogService.getTotal(map);

        JSONObject result = new JSONObject();
        JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor("yyyy-MM-dd"));
        JSONArray jsonArray = JSONArray.fromObject(blogList, jsonConfig);

        result.put("rows", jsonArray);
        result.put("total", total);
        ResponseUtil.write(response, result);
        return null;
    }


    //更新或者新增博客
    @RequestMapping(value = "/save")
    public String saveBlog(Blog blog,HttpServletResponse response) throws Exception {
        int resultTotal = 0;
        if(blog.getId()!=null){
            //更新操作
            resultTotal = blogService.updateBlog(blog);
            //更新索引
            blogIndex.updateIndex(blog);
        }else{
            //新增操作
            resultTotal = blogService.saveBlog(blog);
            //添加索引
            blogIndex.addIndex(blog);
        }
        JSONObject result = new JSONObject();
        if(resultTotal > 0) {
            result.put("success", true);
        } else {
            result.put("success", false);
        }
        ResponseUtil.write(response, result);
        return null;
    }

    //删除博客
    @RequestMapping(value = "delete")
    public String deleteBlog(@RequestParam("ids")String ids,HttpServletResponse response) throws Exception {
        String[] idsStr = ids.split(",");
        for(int i = 0; i < idsStr.length; i++) {
            int id = Integer.parseInt(idsStr[i]);
            //先删除博客所关联的评论 现在没有完成评论的功能 先注释
            //commentService.deleteCommentByBlogId(id);
            blogService.deleteBlog(id);
        }
        JSONObject result = new JSONObject();
        result.put("success", true);
        ResponseUtil.write(response, result);
        return null;
    }

    //通过id获取博客
    @RequestMapping(value = "get")
    public String getById(@RequestParam("id") String id,HttpServletResponse response) throws Exception {

        Blog blog = blogService.getById(Integer.parseInt(id));
        String jsonStr = JSONObject.toJSONString(blog);
        JSONObject result = JSONObject.parseObject(jsonStr);
        ResponseUtil.write(response, result);
        return null;
    }

}
